#!/bin/bash

PROJECT=$1
PHP_VERSION=$2
COMMAND=${@:3}

if [ -z "$PROJECT" ] || [ -z "$PHP_VERSION" ] || [ -z "$COMMAND" ]; then
    echo "Usage: ./docker-cmd.sh <project-name> <php-version> <command>"
    echo "Example: ./docker-cmd.sh project1 php82 composer install"
    echo "Example: ./docker-cmd.sh legacy-project php72 artisan migrate"
    exit 1
fi

if [ ! -d "projects/$PROJECT" ]; then
    echo "Project '$PROJECT' not found!"
    exit 1
fi

case $COMMAND in
    *composer*)
        docker-compose exec -w "/var/www/projects/$PROJECT" $PHP_VERSION $COMMAND
        ;;
    *artisan*)
        docker-compose exec -w "/var/www/projects/$PROJECT" $PHP_VERSION php $COMMAND
        ;;
    "shell")
        docker-compose exec -w "/var/www/projects/$PROJECT" -u www-data $PHP_VERSION bash
        ;;
    *npm*)
        docker-compose run --rm -e CURRENT_PROJECT=$PROJECT node $COMMAND
        ;;
    *)
        echo "Unknown command type"
        exit 1
        ;;
esac 
